<template>
    <div class="app-container">
        <el-dialog :title="props.titleName" v-model="isOpen" width="600px" append-to-body @closed="closed">
            <el-form ref="deptRef" :model="formdata" :rules="formRules" label-width="68">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="formdata.name" placeholder="请输入名称" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm" :loading="isLoading">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
  
<script setup>

import {
    CreateGoodsTag,
    EditGoodsTag,
} from "@/api/goodsManage/goodsLabel"
import { onMounted, reactive, toRefs } from "vue";
import { ElMessage } from 'element-plus'

const props = defineProps({
    titleName: {
        type: String,
        default: ""
    },
    isHide: {
        type: Boolean,
        default: false
    },
    dataInfo: {
        type: Object,
        default: () => { }
    }
})

let isOpen = ref(false)
let isLoading = ref(false)

const emits = defineEmits(['update:isUpdateCreateHide','successAction'])

const cancel = () => {
    isOpen.value = false
}

const closed = () => {
    emits("update:isUpdateCreateHide", false)
}

const createWarehouse = async () => {
    try {
        isLoading.value = true
        let data = await CreateGoodsTag(state.formdata)
        isLoading.value = false
        emits("successAction")
        emits("update:isUpdateCreateHide", false)
        ElMessage.success("新增仓库成功！")
    } catch (error) {
        isLoading.value = false

    }
}

const updateWarehouse = async () => {
    try {
        isLoading.value = true
        let data = await EditGoodsTag(state.formdata)
        isLoading.value = false
        emits("successAction")
        emits("update:isUpdateCreateHide", false)
        ElMessage.success("仓库编辑成功！")
    } catch (error) {
        isLoading.value = false
    }

}

const state = reactive({
    formdata: {
        name: "",
        shortName: "",
        contactUser: "",
        contactPhone: "",
        address: "",
        remark:"",
    },
    formRules: {

    }
})

const submitForm = () => {
    if (props.dataInfo.id) {
        updateWarehouse()
    } else {
        createWarehouse()
    }
}

const { formdata, formRules } = toRefs(state)

onMounted(() => {
    if (props.dataInfo.id) {
        state.formdata = props.dataInfo
    }
    isOpen.value = props.isHide
})
</script>
  